package com.pearl.boot.mp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.base.enums.ResultCodeEnum;
import com.pearl.base.respose.R;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

/**
 * 分页对象统一响应结果集
 *
 * @author TangDan
 * @version 1.0
 * @since 2022/10/27
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PR<T> extends R<T> {
    /**
     * 总数（类型保持和Mybatis Plus一致）
     */
    private long total;

    public static <T> R<List<T>> success(Page<T> page) {
        return response(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), page.getRecords(), page.getTotal());
    }

    private static <T> R<List<T>> response(Integer code, String msg, List<T> data, Long total) {
        PR<List<T>> result = new PR<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        result.setTotal(total);
        return result;
    }

}
